Ext.define('Lms.rms.SoftwareMgmPanel',{
	extend : 'Ext.grid.Panel',
	initComponent : function(){
		Ext.define('SoftwareModel', {
	        extend: 'Ext.data.Model',	        
	        fields: [
	            'id','name', 'type', 'fileName','fileVer','description'
	        ],
	        idProperty: 'id'
	    });
		
		var store = Ext.create('Ext.data.Store',{
			model : 'SoftwareModel',
			sortInfo: {
		        field    : 'name',
		        direction: 'ASC'
		    },
			proxy : {
				type : 'ajax',
				actionMethods : {
					read : 'POST'					
				},
				url : 'software/pageList.do',
				reader : {
					type : 'json',
					root : 'list',
	                totalProperty: 'totalCount'
				}
			}
		});
		
		Ext.apply(this,{
			id : 'SoftwareMgmPanel',
			title : LocaleUtil.getMessage('rms.menu.swMng'),
			columns : this.getColumns(),
			store : store,
			selType: 'checkboxmodel',
			viewConfig : Common.viewConfig(true),
			bbar: Common.pagingToolBar(store),
			columnLines: true,
			tbar : [{
				xtype : 'label',
				text : LocaleUtil.getMessage('rms.label.category')
			},{
				itemId : 'searchCategory',
				xtype : 'combobox',
				name : 'searchCategory',
				store : [['','All'],['CONTENT','CONTENT'],['AGENT ','AGENT ']],
				emptyText : 'All'
			},{
				itemId : 'searchWordTxt',
				xtype : 'textfield',				
				width : 200,
				enableKeyEvents : true,
				maskRe: Common.getMakeRe(),
				listeners : {
					scope : this,
					keypress : function(txtField, e, eOpts){
						if(e.getKey() == 13){
							this.search();
						}
					}
				}
			},{
				xtype : 'button',
				iconCls : 'detail_search',
				text : LocaleUtil.getMessage('search'),
				scope : this,
				handler : this.search
			},'->',{
				xtype : 'button',
				text : LocaleUtil.getMessage('reg'),
				iconCls : 'add',
				hidden : (loginAuth == 'S')?false:true,
				handler : function(){
					Ext.create('Lms.rms.SoftwareInfoDialog').openWindow();
				}
			},{
				xtype : 'button',
				text : LocaleUtil.getMessage('del'),
				iconCls : 'delete',
				hidden : (loginAuth == 'N')?true:false,
				scope : this,
				handler : this.onDeleteHandler
			}]
		});
		this.callParent(arguments);
		this.on('celldblclick',this.onCellClickHandler);
		this.on('show',function(){
			store.load();
		},this);
	},
	
	getColumns : function(){
		return [
		    {text : LocaleUtil.getMessage('rms.label.swName'),dataIndex : 'name'},
		    {text : LocaleUtil.getMessage('rms.label.type'),dataIndex : 'type'},
		    {text : LocaleUtil.getMessage('rms.label.fileName'),dataIndex : 'fileName'},
		    {text : LocaleUtil.getMessage('rms.label.version'),dataIndex : 'fileVer'},
		    {text : LocaleUtil.getMessage('rms.label.description'),dataIndex : 'description', flex:2},
		    {
	        	xtype : 'actioncolumn',
	        	width: 25,
	        	sortable: false,
	        	menuDisabled: true,
	        	items: [{
	        		iconCls: 'save',
	        		scope: this,
	        		handler : this.downloadSoftware
	        	}]
	        }
	    ];
	},
	
	onCellClickHandler : function(grid, td, cellIndex, record, tr, rowIndex, e, eOpts){
		if(cellIndex == 0) return true;
		if(cellIndex == 1){
			Ext.create('Lms.rms.SoftwareInfoDialog').openWindow({id : record.get('id')});
		}
	},
	
	onDeleteHandler : function(){
		var selectedRecords = this.getSelectionModel().getSelection();
		if(selectedRecords.length == 0) return;
		
		Common.confirm(LocaleUtil.getMessage('confirm'),LocaleUtil.getMessage('rms.msg.doYouWantToDeleteSelectedSoftware'),function(btn){
			if(btn == 'yes'){
				var ids = [];
				for ( var i = 0; i < selectedRecords.length; i++) {
					ids[i] = selectedRecords[i].get('id'); 
				}
				Ext.Ajax.request({
		            url:'software/delete.do',
		            params: {
		            	ids : ids
		            },
		            scope : this,
		            success : function(result, request){
		                this.getStore().reload();
		            },
		            failure: Common.failureMessage
		        });
			}
		},this);
	},
	
	search : function(){
		var searchCategory = this.queryById('searchCategory').getValue();
		var searchWordTxt = this.queryById('searchWordTxt').getValue();		
		this.getStore().load({
			params : {
				type : searchCategory,
				name : searchWordTxt
			}
		});
	},
	
	downloadSoftware : function(grid, rowIndex, colIndex){
		var selectedRecord = grid.getStore().getAt(rowIndex);
		Common.downloadIframe('software/downloadSoftware.do?id='+selectedRecord.get('id'));
	},
});